import json

import requests
from ttzScrapyProject.constants import HttpConstant
from ttzScrapyProject.constants import StatusCodeConstant


def get(operation, url, headers=None):
    if headers is None:
        # USER_AGENTS = [
        #     'Mozilla/5.0 (Linux; Android 11; Pixel 4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.105 Mobile Safari/537.36',
        #     'Mozilla/5.0 (iPad; CPU OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1',
        #     'Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; Microsoft; Lumia 950) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36 Edge/16.16299'
        # ]
        headerList  =[
            {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"},
            {"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Mobile Safari/537.36"},
            {"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0 Mobile/15C50 Safari/604.1 Chrome/63.0.3239.111"},
            {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"},
            {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"},
            {"User-Agent": "Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPM1.171019.011) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Edge/18.0"},
            {"User-Agent": "Mozilla/5.0 (Linux; Android 8.1.0; Pixel 3 XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.99 Mobile Safari/537.36"},
        ]
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
        }
    response = requests.get(url, headers=headers)
    try:
        print(f"{operation}请求成功,地址{url},状态吗{response.status_code},请求头{headers}")
        if response.status_code != StatusCodeConstant.SUCCESS_CODE:
            print(f"{operation}请求失败,地址{url}")
        data = json.loads((response.content))
        if data["Code"] == StatusCodeConstant.SUCCESS_CODE:
            return data
        else:
            print(f"{operation}请求失败,地址{url},响应数据{data}")
            return ""
    except:
        print(f"{operation}请求失败,地址{url}")
        return ""


def parseResponseData(self, response):
    successCode = self.settings.get("SUCCESS_CODE")
    data = json.loads((response.body))
    print(data["Code"])
    if data["Code"] == successCode:
        return data
    else:
        return ""


# 测试请求头
# if __name__ == '__main__':
#     headerList  =[
#         {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"},
#         {"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Mobile Safari/537.36"},
#         {"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0 Mobile/15C50 Safari/604.1 Chrome/63.0.3239.111"},
#         {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"},
#         {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"},
#         {"User-Agent": "Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPM1.171019.011) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Edge/18.0"},
#         {"User-Agent": "Mozilla/5.0 (Linux; Android 8.1.0; Pixel 3 XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.99 Mobile Safari/537.36"},
#     ]
#     print(get("获取百度信息","https://www.baidu.com/"))
#     print(get("获取百度信息","https://www.baidu.com/",headerList[0]))
#     print(get("获取百度信息","https://www.baidu.com/",headerList[1]))
#     print(get("获取百度信息","https://www.baidu.com/",headerList[2]))
#     print(get("获取百度信息","https://www.baidu.com/",headerList[3]))
#     print(get("获取百度信息","https://www.baidu.com/",headerList[4]))
#     print(get("获取百度信息","https://www.baidu.com/",headerList[5]))
#     print(get("获取百度信息","https://www.baidu.com/",headerList[6]))
#     print(get("获取百度信息","https://www.baidu.com/"))